using JSOCTNet.IBIZ;
using JSOCTNetSDK.XmppModelFun;
using jsxmpp;
using System;
using System.Collections;
using System.Collections.Generic;

namespace JSOCTNetSDK.XmppTask
{
	public class WCFManage : System.IDisposable
	{
		private static System.Collections.Generic.Dictionary<string, string> m_dicDevState = new System.Collections.Generic.Dictionary<string, string>();

		private IXmppSend iXmppSend = FactorySync.CreateIXmppSend();

		private IXmppSend iXmppSendThird = FactorySync.CreateIXmppSendThird();

		private IXmppDown iXmppDown = FactorySync.CreateIXmppDown();

		private void UpEvent(string json)
		{
			ServiceResponseData serviceResponseData = new ServiceResponseData();
			ServiceRequestParam serviceRequestParam = new ServiceRequestParam();
			try
			{
				JsonToModel.GetModel(json, ref serviceRequestParam);
			}
			catch (System.Exception ex)
			{
				XmppLogHelper.LogSDKUpdateData.Error("XMPP服务解析事件上传数据失败", ex);
			}
			if (serviceRequestParam.serviceId == "NISSP_ACION_EQUIPMENT_STATE")
			{
				if (!ConfigFile.IsPrintXMPPDeviceStatus)
				{
					return;
				}
				XmppLogHelper.LogSDKDeviceStatus.Info("XMPP服务----->接收到设备状态" + json);
			}
			else
			{
				XmppLogHelper.LogSDKUpdateData.Info("XMPP服务----->接收到需要上传的事件：" + json);
			}
			if (ConfigFile.IsStartXmpp)
			{
				serviceResponseData = this.iXmppSend.requestService(serviceRequestParam, serviceRequestParam.serviceId, "", "");
				if (serviceResponseData != null)
				{
					if (serviceResponseData.resultCode == 0)
					{
						XmppLogHelper.LogSDKUpdateData.Info("XMPP服务----->上传XMPP服务事件成功");
					}
					else
					{
						XmppLogHelper.LogSDKUpdateData.Error("XMPP服务----->上传XMPP服务事件失败，返回错误码为：" + serviceResponseData.resultCode);
						XmppLogHelper.LogSDKUpdateData.Error("XMPP服务----->上传XMPP服务事件失败，返回数据为：" + ModelToJson.GetJson(serviceResponseData));
					}
				}
				else
				{
					XmppLogHelper.LogSDKUpdateData.Error("XMPP服务----->上传XMPP服务事件失败，返回数据为空");
				}
			}
			if (ConfigFile.IsStartThirdParty)
			{
				serviceResponseData = this.iXmppSendThird.requestService(serviceRequestParam, serviceRequestParam.serviceId, "", "");
				if (serviceResponseData != null)
				{
					if (serviceResponseData.resultCode == 0)
					{
						XmppLogHelper.LogSDKThirdUpdateData.Info("XMPP服务----->上传第三方XMPP服务事件成功");
						return;
					}
					XmppLogHelper.LogSDKThirdUpdateData.Error("XMPP服务----->上传第三方XMPP服务事件失败，返回错误码为：" + serviceResponseData.resultCode);
					XmppLogHelper.LogSDKThirdUpdateData.Error("XMPP服务----->上传第三方XMPP服务事件失败，返回数据为：" + ModelToJson.GetJson(serviceResponseData));
					return;
				}
				else
				{
					XmppLogHelper.LogSDKThirdUpdateData.Error("XMPP服务----->上传第三方XMPP服务事件失败，返回数据为空");
				}
			}
		}

		private string UpEventHasReturn(string json)
		{
			ServiceResponseData serviceResponseData = new ServiceResponseData();
			ServiceRequestParam serviceRequestParam = new ServiceRequestParam();
			string result = "";
			try
			{
				JsonToModel.GetModel(json, ref serviceRequestParam);
			}
			catch (System.Exception ex)
			{
				XmppLogHelper.LogSDKUpdateData.Error("XMPP服务解析事件上传数据失败", ex);
				return "";
			}
			if (serviceRequestParam.serviceId == "NISSP_ACION_EQUIPMENT_STATE")
			{
				if (ConfigFile.IsPrintXMPPDeviceStatus)
				{
					XmppLogHelper.LogSDKDeviceStatus.Info("XMPP服务----->接收到设备状态" + json);
				}
			}
			else
			{
				XmppLogHelper.LogSDKUpdateData.Info("XMPP服务----->接收到需要上传的事件：" + json);
			}
			if (ConfigFile.IsStartXmpp)
			{
				serviceResponseData = this.iXmppSend.requestService(serviceRequestParam, serviceRequestParam.serviceId, "", "");
				if (serviceResponseData == null)
				{
					XmppLogHelper.LogSDKUpdateData.Error("XMPP服务----->上传XMPP服务事件失败，返回数据为空");
					return "";
				}
				if (serviceResponseData.resultCode == 0)
				{
					XmppLogHelper.LogSDKUpdateData.Info("XMPP服务----->上传XMPP服务事件成功");
				}
				else
				{
					XmppLogHelper.LogSDKUpdateData.Error("XMPP服务----->上传XMPP服务事件失败，返回错误码为：" + serviceResponseData.resultCode);
					XmppLogHelper.LogSDKUpdateData.Error("XMPP服务----->上传XMPP服务事件失败，返回数据为：" + ModelToJson.GetJson(serviceResponseData));
				}
				result = ModelToJson.GetJson(serviceResponseData);
			}
			if (ConfigFile.IsStartThirdParty)
			{
				serviceResponseData = this.iXmppSendThird.requestService(serviceRequestParam, serviceRequestParam.serviceId, "", "");
				if (serviceResponseData == null)
				{
					XmppLogHelper.LogSDKThirdUpdateData.Error("XMPP服务----->上传第三方XMPP服务事件失败，返回数据为空");
					return "";
				}
				if (serviceResponseData.resultCode == 0)
				{
					XmppLogHelper.LogSDKThirdUpdateData.Info("XMPP服务----->上传第三方XMPP服务事件成功");
				}
				else
				{
					XmppLogHelper.LogSDKThirdUpdateData.Error("XMPP服务----->上传第三方XMPP服务事件失败，返回错误码为：" + serviceResponseData.resultCode);
					XmppLogHelper.LogSDKThirdUpdateData.Error("XMPP服务----->上传第三方XMPP服务事件失败，返回数据为：" + ModelToJson.GetJson(serviceResponseData));
				}
				result = ModelToJson.GetJson(serviceResponseData);
			}
			return result;
		}

		public void Dispose()
		{
		}

		public string RequestDown(string json)
		{
			XmppLogHelper.LogSDKDownData.Info("WCF服务接收下发数据请求：" + json);
			ServiceRequestParam request = new ServiceRequestParam();
			try
			{
				JsonToModel.GetModel(json, ref request);
			}
			catch (System.Exception ex)
			{
				XmppLogHelper.LogSDKUpdateData.Error("WCF服务接收下发数据转换失败", ex);
				string result = "WCF服务接收下发数据转换失败";
				return result;
			}
			if (this.iXmppDown == null)
			{
				XmppLogHelper.LogSDKDownData.Error("下发数据处理接口为空未执行！");
				return "下发数据处理接口为空未执行";
			}
			ServiceResponseData serviceResponseData = this.iXmppDown.DownRequestProcess(request);
			if (serviceResponseData != null)
			{
				json = ModelToJson.GetJson(serviceResponseData);
				XmppLogHelper.LogSDKUpdateData.Info("WCF服务执行下发返回：" + json);
				return json;
			}
			XmppLogHelper.LogSDKUpdateData.Error("WCF服务执行下发返回数据为空：" + json);
			return "WCF服务执行下发返回数据为空";
		}

		public void RequestUpEvent(string json)
		{
			if (json.IndexOf("NISSP_ACION_EQUIPMENT_STATE") > 0)
			{
				ServiceRequestParam serviceRequestParam = new ServiceRequestParam();
				JsonToModel.GetModel(json, ref serviceRequestParam);
				bool flag = true;
				try
				{
					if (serviceRequestParam != null && serviceRequestParam.dataItems != null && serviceRequestParam.dataItems.Count > 0)
					{
						string value = serviceRequestParam.dataItems[0].attributes["EQUIPMENT_ID"].ToString();
						string value2 = serviceRequestParam.dataItems[0].attributes["STATE"].ToString();
						if (string.IsNullOrWhiteSpace(value) || string.IsNullOrWhiteSpace(value2))
						{
							flag = false;
						}
					}
				}
				catch (System.Exception ex)
				{
					XmppLogHelper.LogSDKUpdateData.Error("WCF转换上传设备状态错误", ex);
				}
				if (flag)
				{
					this.UpEvent(json);
					return;
				}
			}
			else
			{
				this.UpEvent(json);
			}
		}

		public string RequestUpEventHasReturn(string json)
		{
			if (json.IndexOf("NISSP_ACION_EQUIPMENT_STATE") <= 0)
			{
				return this.UpEventHasReturn(json);
			}
			ServiceRequestParam serviceRequestParam = new ServiceRequestParam();
			JsonToModel.GetModel(json, ref serviceRequestParam);
			bool flag = true;
			try
			{
				if (serviceRequestParam != null && serviceRequestParam.dataItems != null && serviceRequestParam.dataItems.Count > 0)
				{
					string text = serviceRequestParam.dataItems[0].attributes["EQUIPMENT_ID"].ToString();
					string text2 = serviceRequestParam.dataItems[0].attributes["STATE"].ToString();
					if (string.IsNullOrWhiteSpace(text) || string.IsNullOrWhiteSpace(text2))
					{
						flag = false;
					}
					if (WCFManage.m_dicDevState.ContainsKey(text))
					{
						string[] array = WCFManage.m_dicDevState[text].ToString().Split(new char[]
						{
							'|'
						});
						if (array != null && array.Length == 2)
						{
							if (array[1] != text2)
							{
								WCFManage.m_dicDevState[text] = string.Format("{0}|{1}", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), text2);
							}
							else
							{
								flag = false;
							}
						}
					}
					else
					{
						WCFManage.m_dicDevState.Add(text, string.Format("{0}|{1}", System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff"), text2));
					}
				}
			}
			catch
			{
			}
			if (flag)
			{
				return this.UpEventHasReturn(json);
			}
			return string.Empty;
		}

		public string HttpGet(string strUrl)
		{
			if (ConfigFile.IsPrintlog)
			{
				XmppLogHelper.LogSDK.Info(strUrl);
			}
			string text = TaskManage.Instance.HttpGet(strUrl);
			if (ConfigFile.IsPrintlog)
			{
				XmppLogHelper.LogSDK.Info(text);
			}
			return text;
		}

		public string HttpPost(string strUrl, string strPostContent)
		{
			return TaskManage.Instance.HttpPost(strUrl, strPostContent);
		}

		public string Request1631FromZZJF(string url)
		{
			if (ConfigFile.IsPrintlog)
			{
				XmppLogHelper.LogSDK.Info(url);
			}
			string text = "";
			ServiceRequestParam request = this.BuildServiceRequestParam(url);
			ServiceResponseData serviceResponseData = TaskManage.Instance.DownRequestProcess(request);
			if (serviceResponseData != null && serviceResponseData.dataItems.Count > 0)
			{
				if (serviceResponseData.message != "1631子系统初始化失败")
				{
					text = serviceResponseData.dataItems[0].attributes["RESULT"].ToString();
				}
				else
				{
					text = "1631子系统初始化失败";
				}
			}
			if (ConfigFile.IsPrintlog)
			{
				XmppLogHelper.LogSDK.Info(text);
			}
			return text;
		}

		private ServiceRequestParam BuildServiceRequestParam(string url)
		{
			if (ConfigFile.IsPrintlog)
			{
				XmppLogHelper.LogSDK.Info(url);
			}
			ServiceRequestParam serviceRequestParam = new ServiceRequestParam();
			serviceRequestParam.serviceId = this.GetTaskCode(url);
			serviceRequestParam.requestType = "DATA";
			serviceRequestParam.dataItems = new System.Collections.Generic.List<TXDataObject>();
			TXDataObject tXDataObject = new TXDataObject();
			tXDataObject = new TXDataObject();
			tXDataObject.attributes = new System.Collections.Hashtable();
			tXDataObject.attributes.Add("URL", url);
			serviceRequestParam.dataItems.Add(tXDataObject);
			if (ConfigFile.IsPrintlog)
			{
				string json = ModelToJson.GetJson(serviceRequestParam);
				XmppLogHelper.LogSDKUpdateData.Info(json);
			}
			return serviceRequestParam;
		}

		private string GetTaskCode(string url)
		{
			string result = "NISSP_JSPAY_ORDER";
			if (url.IndexOf("BookSearch") > -1)
			{
				result = "NISSP_JSPAY_ORDER";
			}
			return result;
		}
	}
}
